package com.uiao.activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

import javax.jms.*;

/**
 * 点对点队列发送端
 *
 * Created by uiao on 2018/3/10.
 */
public class QueueProducer {

    public static Logger logger = Logger.getLogger(QueueProducer.class);

    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        Session session;
        Destination destination;
        MessageProducer messageProducer;
        // 在这可以根据用户名username和密码password以及url使用有参的构造函数创建链接工厂
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);

        try {
            connection = connectionFactory.createConnection();
            connection.start();

            // true表示可以支持事务，后面的自动确认为Session.SESSION_TRANSACTED
            //第一个参数为false时，paramB的值可为Session.AUTO_ACKNOWLEDGE，Session.CLIENT_ACKNOWLEDGE，DUPS_OK_ACKNOWLEDGE其中一个。
            //Session.AUTO_ACKNOWLEDGE为自动确认。客户端发送和接收消息不需要做额外的工作。哪怕是接收端发生异常，也会被当作正常发送成功。
            //Session.CLIENT_ACKNOWLEDGE为客户端确认。客户端接收到消息后，必须调用javax.jms.Message的acknowledge方法。jms服务器才会当作发送成功，并删除消息。
            //DUPS_OK_ACKNOWLEDGE允许副本的确认模式。一旦接收方应用程序的方法调用从处理消息处返回，会话对象就会确认消息的接收；而且允许重复确认。
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("mq-queue");

            messageProducer = session.createProducer(destination);

            //DeliveryMode.PERSISTENT 当activemq关闭的时候，队列数据将会被保存
            //DeliveryMode.NON_PERSISTENT 当activemq关闭的时候，队列里面的数据将会被清空
            messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

            sendMessage(session, messageProducer);
            messageProducer.setTimeToLive(10000);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void sendMessage(Session session, MessageProducer messageProducer) throws Exception {
        for (int i = 0; i < 10; i++) {
            Thread.sleep(100);

            // 消息转化器可以自定义或者用jmsMessagingTemplate
            TextMessage textMessage = session.createTextMessage("第 " + i + " 条文本消息");

            // 传递序列化对象,传递javaBean，TopicProducer就是用的这个
            MqBean bean = new MqBean();
            bean.setAge(24);
            bean.setName("uiao");
            ObjectMessage objectMessage = session.createObjectMessage(bean);

            // 传递流，用来传递文件
            StreamMessage streamMessage = session.createStreamMessage();

            // 传递map
            MapMessage mapMessage = session.createMapMessage();

            // 传递文件
            BytesMessage bytesMessage = session.createBytesMessage();

            logger.info("发送消息：" + textMessage.getText());
            messageProducer.send(textMessage);
        }
    }
}
